package com.gx.project.controller;

import com.gx.common.result.ResponsePageResult;
import com.gx.common.result.ResponseResult;
import com.gx.project.po.Goods;
import com.gx.project.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.HashMap;

/**
 * @author gx
 * @description 商品
 */

@RestController
@RequestMapping("/goods")
@Validated
public class GoodsContoller {


    @Autowired
    private GoodsService goodsService;

    /**
     * 查询商品列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/getAllGoods")
    public ResponsePageResult getAllGoods(@NotNull(message = "pageNo必填") int pageNo,  @NotNull(message = "pageSize必填")int pageSize) {
        try {
            return goodsService.getListByPage(new HashMap<>(),pageNo, pageSize);
        } catch (Exception ex) {
            return ResponsePageResult.failInstance(ex.getMessage());
        }
    }


    /**
     * 查询商品详情
     * @param goodId
     * @return
     */
    @RequestMapping("/getGoodsById")
    public ResponseResult getGoodsById(@NotNull(message = "goodId必填") Long goodId) {
        try {
            ResponseResult result = ResponseResult.successInstance();
            result.setData(goodsService.getById(goodId));
            return result;
        } catch (Exception ex) {
            return ResponseResult.failInstance(ex.getMessage());
        }
    }

    /**
     * 添加商品
     */
    @RequestMapping("/addGoods")
    public ResponseResult addGoods(@NotNull(message = "goodName必填") String goodName,
                                   @NotNull(message = "goodPrice必填") double goodPrice,
                                   @NotNull(message = "goodStock必填") Long goodStock,
                                   @NotNull(message = "goodDescription必填") String goodDescription,
                                   @NotNull(message = "goodCategoryId必填") Long goodCategoryId,
                                   @NotNull(message = "goodBrandId必填") Long goodBrandId,
                                   @NotNull(message = "goodPicUrl必填") String goodPicUrl) {
        try {
            Goods goods = new Goods();
            goods.setGoodName(goodName);
            goods.setGoodPrice(BigDecimal.valueOf(goodPrice));
            goods.setGoodStock(goodStock);
            goods.setGoodDescription(goodDescription);
            goods.setGoodCategoryId(goodCategoryId);
            goods.setGoodBrandId(goodBrandId);
            goods.setGoodImageUrl(goodPicUrl);
            goodsService.saveOrUpdate(goods);
            return ResponseResult.successInstance();
        } catch (Exception ex) {
            return ResponseResult.failInstance(ex.getMessage());
        }
    }

    /**
     * 修改商品
     */
    @RequestMapping("/updateGoods")
    public ResponseResult updateGoods(@NotNull(message = "goodName必填") String goodName,
                                      @NotNull(message = "goodPrice必填") double goodPrice,
                                      @NotNull(message = "goodStock必填") Long goodStock,
                                      @NotNull(message = "goodDescription必填") String goodDescription,
                                      @NotNull(message = "goodCategoryId必填") Long goodCategoryId,
                                      @NotNull(message = "goodBrandId必填") Long goodBrandId,
                                      @NotNull(message = "goodPicUrl必填") String goodPicUrl) {
        try {
            Goods goods = new Goods();
            goods.setGoodName(goodName);
            goods.setGoodPrice(BigDecimal.valueOf(goodPrice));
            goods.setGoodStock(goodStock);
            goods.setGoodDescription(goodDescription);
            goods.setGoodCategoryId(goodCategoryId);
            goods.setGoodBrandId(goodBrandId);
            goods.setGoodImageUrl(goodPicUrl);
            goodsService.saveOrUpdate(goods);
            return ResponseResult.successInstance();
        } catch (Exception ex) {
            return ResponseResult.failInstance(ex.getMessage());
        }
    }

    /**
     * 删除商品
     */
    @RequestMapping("/deleteGoods")
    public ResponseResult deleteGoods(@NotNull(message = "id必填") Long goodId) {
        try {
            goodsService.removeById(goodId);
            return ResponseResult.successInstance();
        } catch (Exception ex) {
            return ResponseResult.failInstance(ex.getMessage());
        }
    }










}
